package com.pug.service.config.mq;

import com.pug.utils.FastJsonUtil;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Author: lqm
 * create: 2022/4/3 19:53
 * Lover: lqx
 *
 * @see
 */
@Configuration
public class OrderDirectConfig {
    public static final String order_direct_queue="order_direct_queue";
    public static final String order_direct_dead_queue="order_direct_dead_queue";
    public static final String order_direct_exchange="order_direct_exchange";
    public static final String order_direct_dead_exchange="order_direct_dead_exchange";
    public static final String order_direct_key="order_direct_key";
    public static final String order_direct_dead_key="order_direct_dead_key";
    public static final String weiXin_direct_queue="weiXin_direct_queue";
    public static final String weiXin_direct_key="weiXin_direct_key";
    //普通交换机
    @Bean
    public Exchange OrderDirectExchange(){
        return new DirectExchange(order_direct_exchange,true,false); //最后还可以带参数
    }
    //死信交换机
    @Bean
    public Exchange OrderDirectDeadExchange(){
        return new DirectExchange(order_direct_dead_exchange,true,false); //最后还可以带参数
    }
    //普通队列
    @Bean
    public Queue OrderDirectQueue(){
        return new Queue(order_direct_queue,true,false, false);
    }
    //死信队列
    @Bean
    public Queue OrderDeadDirectQueue(){
        Map<String,Object> args=new HashMap<>();
        //死信交换机
        args.put("x-dead-letter-exchange",order_direct_dead_exchange);
        //死信路由
        args.put("x-dead-letter-routing-key",order_direct_dead_key);
        //过期时间
        args.put("x-message-ttl",10000);
        return new Queue(order_direct_dead_queue,true,false, false,args);
    }
    //绑定
    @Bean
    public Binding bindOrderDead(){
        return BindingBuilder.bind(OrderDeadDirectQueue()).to(OrderDirectExchange()).with(order_direct_key).noargs();
    }
    @Bean
    public Binding bindOrderConsumer(){
        return BindingBuilder.bind(OrderDirectQueue()).to(OrderDirectDeadExchange()).with(order_direct_dead_key).noargs();
    }
    //没有微信支付的伪支付
    @Bean
    public Queue WeiXinOrderQueue(){
        return new Queue(weiXin_direct_queue,true,false,false);
    }
    //绑定队列和交换机
    @Bean
    public Binding bindWeiXinOrder(){
        return BindingBuilder.bind(WeiXinOrderQueue()).to(OrderDirectExchange()).with(weiXin_direct_key).noargs();
    }
}
